package com.yckj.dao;

import com.yckj.Util.DbUtilHikari;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import cn.hutool.crypto.SecureUtil;

/**
 * 对用户表的操作：注册用户、删除用户
 * @author lujingye
 * @version 1.0
 * @return 注册成功返回1，否则返回0
 */
public class UserDao {
    public int insertUser(String uname,String upsw){
        //1，声明一些变量备用
        //代表数据库链接
        Connection connection;
        //预编译对象，用来执行SQL语句
        PreparedStatement preparedStatement = null;
        int flag = 0;

        //2,。获取连接
        connection = DbUtilHikari.getConnection();
        //3.定义并准备SQL语句
        String sql = "INSEOR INTO users(username,userpsw) VALUES(?,?)";
        try {
            preparedStatement = connection.prepareStatement(sql);
            //4.给？赋值
            preparedStatement.setString(1,uname);
            preparedStatement.setString(2,SecureUtil.md5(upsw));
            //5.执行SQL语句
            flag = preparedStatement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            //关闭数据库资源
            DbUtilHikari.close(preparedStatement,connection);
        }
        return flag;
    }
}